from fastapi import FastAPI
from starlette.staticfiles import StaticFiles
from app.config import settings
from fastapi.middleware.cors import CORSMiddleware
import logging
from app.routers.activities import router as activities_router
from app.routers.events import router as events_router
from app.routers.evaluations import router as evaluations_router
from app.routers.indicators import router as indicators_router
from app.routers.reports import router as reports_router
from app.routers.evaluation_templates import router as evaluation_templates_router
from app.routers.llm_config import router as llm_config_router
from app.routers.template_outline import router as template_outline_router
from app.routers.dictionaries import router as dictionaries_router
from app.routers.health import router as health_router
from app.routers.auth import router as auth_router
from app.routers.users import router as users_router
from app.routers.roles import router as roles_router
from app.routers.menus import router as menus_router
from app.routers.report_headers import router as report_headers_router
from app.routers.report_versions import router as report_versions_router
from app.routers.report_content import router as report_content_router
from app.routers.section_conversations import router as section_conversations_router
from app.routers.network_volume import router as network_volume_router
from app.routers.social_topics import router as social_topics_router
from app.routers.social_tasks import router as social_tasks_router
from app.routers.social_contents import router as social_contents_router
from app.routers.file_assets import router as file_assets_router


# 配置日志 - 强制重新配置
for handler in logging.root.handlers[:]:
    logging.root.removeHandler(handler)

logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.StreamHandler(),
        logging.FileHandler('app.log', encoding='utf-8')
    ],
    force=True  # 强制重新配置日志
)

# 确保indicators模块的日志级别
indicators_logger = logging.getLogger('indicators')
indicators_logger.setLevel(logging.DEBUG)

# 测试日志
logging.info("=== FastAPI应用启动 ===")
logging.getLogger('indicators').info("=== indicators日志记录器测试 ===")

app = FastAPI(title="FAMES 后端服务", version="0.1.0")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

app.include_router(health_router)
app.include_router(auth_router)
app.include_router(activities_router)
app.include_router(events_router)
app.include_router(evaluations_router)
app.include_router(indicators_router)
app.include_router(reports_router)
app.include_router(evaluation_templates_router)
app.include_router(llm_config_router)
app.include_router(template_outline_router)
app.include_router(dictionaries_router)
app.include_router(users_router)
app.include_router(roles_router)
app.include_router(menus_router)
app.include_router(report_headers_router)
app.include_router(report_versions_router)
app.include_router(report_content_router)
app.include_router(section_conversations_router)
app.include_router(network_volume_router)
app.include_router(file_assets_router)
app.include_router(social_topics_router)
app.include_router(social_tasks_router)
app.include_router(social_contents_router)

# 静态文件：导出报告存储目录
app.mount("/static/reports", StaticFiles(directory=str(settings.reports_dir)), name="reports_static")

if __name__ == "__main__":
    import uvicorn
    uvicorn.run("app.main:app", host="0.0.0.0", port=8000, reload=True)
